﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TgaScanner
{
    internal class ScanDirectoryCommand : BaseCommand
    {
        private readonly MainWindowViewModel viewModel;

        public ScanDirectoryCommand(MainWindowViewModel viewModel)
        {
            this.viewModel = viewModel;
        }

        public override void Execute(object? parameter)
        {
            _Execute(parameter);
        }

        private async void _Execute(object? parameter)
        { 
            string selectedDirectory = viewModel.SelectedDirectory;

            if (string.IsNullOrEmpty(selectedDirectory) || !Directory.Exists(selectedDirectory))
            {
                return;
            }

            viewModel.StatusText = $"正在扫描，请耐心等待……";

            await Task.Run(() =>
            {
                var tgaFiles = Directory.GetFiles(selectedDirectory, "*.tga", SearchOption.AllDirectories);

                // 更新 UI 线程中的 ObservableCollection
                Application.Current.Dispatcher.Invoke(() =>
                {
                    viewModel.StatusText = $"扫描到 {tgaFiles.Length} 个文件，正在分析……";
                });

                var fileItems = new ObservableCollection<FileItem>();
                int count = 0;

                foreach (var tgaFile in tgaFiles)
                {
                    string filepath = Path.GetFullPath(tgaFile);
                    int width = 0;
                    int height = 0;
                    int channels = 0;
                    using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                    {
                        using (var binaryReader = new BinaryReader(fileStream))
                        {
                            binaryReader.ReadBytes(12);
                            width = binaryReader.ReadUInt16();
                            height = binaryReader.ReadUInt16();
                            channels = binaryReader.ReadByte() / 8;
                        }
                    }

                    fileItems.Add(new FileItem
                    {
                        FileName = filepath,
                        Width = width,
                        Height = height,
                        Channels = channels
                    });

                    if (++count % 100 == 0)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            viewModel.StatusText = $"分析进行中，当前进度：{count} / {tgaFiles.Length}。";
                        });
                    }
                }

                Application.Current.Dispatcher.Invoke(() =>
                {
                    viewModel.FileItems.Clear();

                    foreach (var fileItem in fileItems)
                    {
                        viewModel.FileItems.Add(fileItem);
                    }
                });
            });

            viewModel.StatusText = $"扫描完成，总的文件数目为：{viewModel.FileItems.Count}";
        }
    }
}
